package icu.xuyijie.springbootlearning1.chapter3.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author 徐一杰
 * @date 2025/02/25 10:44
 * @description 测试jdbc的使用
 */
@RestController
@RequestMapping("/jdbc")
public class JdbcController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/query")
    public void query() {
        // 定义查询出来的数据格式
        RowMapper<MyEntity> rowMapper = new BeanPropertyRowMapper<>(MyEntity.class);

        // 获取查询结果
        List<MyEntity> list = jdbcTemplate.query("SELECT * FROM test", rowMapper);

        System.out.println(list);
    }

    @RequestMapping("/add")
    public void add() {
        jdbcTemplate.update("INSERT INTO test (name, create_time) VALUES (?, ?)", "asdasdas", new Date());
    }

    @RequestMapping("/update")
    public void update() {
        jdbcTemplate.update("UPDATE test SET name = ? WHERE id = ?", "12312312", 7);
    }

    @RequestMapping("/del")
    public void del() {
        jdbcTemplate.update("DELETE FROM test WHERE id = ?", 7);
    }

}
